package it.stasbranger.myproject.client.managed.activity;

import com.google.gwt.activity.shared.Activity;

import com.google.gwt.place.shared.PlaceController;

import com.google.gwt.requestfactory.shared.EntityProxyId;
import com.google.gwt.requestfactory.shared.RequestContext;

import it.stasbranger.myproject.client.managed.request.SuppliersProxy;
import it.stasbranger.myproject.client.managed.request.SuppliersRequest;

import it.stasbranger.myproject.client.managed.ui.SuppliersDetailsView;
import it.stasbranger.myproject.client.managed.ui.SuppliersEditView;
import it.stasbranger.myproject.client.managed.ui.SuppliersMobileEditView;

import it.stasbranger.myproject.client.scaffold.ScaffoldApp;

import it.stasbranger.myproject.client.scaffold.place.CreateAndEditProxy;
import it.stasbranger.myproject.client.scaffold.place.FindAndEditProxy;
import it.stasbranger.myproject.client.scaffold.place.ProxyPlace;

public class SuppliersActivitiesMapper {
	private final it.stasbranger.myproject.client.managed.request.ApplicationRequestFactory requests;
	private final PlaceController placeController;

	public SuppliersActivitiesMapper(it.stasbranger.myproject.client.managed.request.ApplicationRequestFactory requests, PlaceController placeController) {
		this.requests = requests;
		this.placeController = placeController;
	}

	public Activity getActivity(ProxyPlace place) {
		switch (place.getOperation()) {
		case DETAILS:
			return new it.stasbranger.myproject.client.managed.activity.SuppliersDetailsActivity((EntityProxyId<SuppliersProxy>) place.getProxyId(), requests, placeController, ScaffoldApp.isMobile() ? it.stasbranger.myproject.client.managed.ui.SuppliersMobileDetailsView.instance() : SuppliersDetailsView.instance());

		case EDIT:
			return makeEditActivity(place);

		case CREATE:
			return makeCreateActivity();
		}

		throw new IllegalArgumentException("Unknown operation " + place.getOperation());
	}

	@SuppressWarnings("unchecked")
	private EntityProxyId<it.stasbranger.myproject.client.managed.request.SuppliersProxy> coerceId(it.stasbranger.myproject.client.scaffold.place.ProxyPlace place) {
		return (EntityProxyId<it.stasbranger.myproject.client.managed.request.SuppliersProxy>) place.getProxyId();
	}

	private Activity makeCreateActivity() {
		it.stasbranger.myproject.client.managed.ui.SuppliersEditView.instance().setCreating(true);
		final it.stasbranger.myproject.client.managed.request.SuppliersRequest request = requests.suppliersRequest();
		Activity activity = new CreateAndEditProxy<SuppliersProxy>(SuppliersProxy.class, request, ScaffoldApp.isMobile() ? SuppliersMobileEditView.instance() : SuppliersEditView.instance(), placeController) {

			@Override
			protected RequestContext createSaveRequest(SuppliersProxy proxy) {
				request.saveSuppliers(proxy);
				return request;
			}
		};

		return new it.stasbranger.myproject.client.managed.activity.SuppliersEditActivityWrapper(requests, it.stasbranger.myproject.client.scaffold.ScaffoldApp.isMobile() ? it.stasbranger.myproject.client.managed.ui.SuppliersMobileEditView.instance() : it.stasbranger.myproject.client.managed.ui.SuppliersEditView.instance(), activity, null);
	}

	private Activity makeEditActivity(ProxyPlace place) {
		it.stasbranger.myproject.client.managed.ui.SuppliersEditView.instance().setCreating(false);
		EntityProxyId<SuppliersProxy> proxyId = coerceId(place);
		Activity activity = new FindAndEditProxy<SuppliersProxy>(proxyId, requests, ScaffoldApp.isMobile() ? it.stasbranger.myproject.client.managed.ui.SuppliersMobileEditView.instance() : it.stasbranger.myproject.client.managed.ui.SuppliersEditView.instance(), placeController) {

			@Override
			protected RequestContext createSaveRequest(it.stasbranger.myproject.client.managed.request.SuppliersProxy proxy) {
				SuppliersRequest request = requests.suppliersRequest();
				request.saveSuppliers(proxy);
				return request;
			}
		};

		return new SuppliersEditActivityWrapper(requests, ScaffoldApp.isMobile() ? it.stasbranger.myproject.client.managed.ui.SuppliersMobileEditView.instance() : it.stasbranger.myproject.client.managed.ui.SuppliersEditView.instance(), activity, proxyId);
	}
}
